using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DAO;
using DTO;

namespace BUS
{
    public enum PhieuMuonSachBUSResult
    {
        OK, DAOERROR
    }
    public class PhieuMuonSach
    {
        public static int PhatSinhMa()
        {
            DAO.PhieuMuonSach dao = new DAO.PhieuMuonSach();
            string sql = "SELECT TOP 1 MaPhieu FROM PhieuMuonSach ORDER BY MaPhieu DESC";

            DataTable dt = dao.Select(sql);
            if (dt.Rows.Count == 0)
                return 1;
            else
                return (int.Parse(dt.Rows[0][0].ToString()) + 1);
        }
       public static PhieuMuonSachBUSResult Them(DTO.PhieuMuonSach s)
        {
            DAO.PhieuMuonSach pms = new DAO.PhieuMuonSach();
            string sql = "INSERT INTO PhieuMuonSach VALUES(";
            sql += s.MaPhieuMuonSach + ", ";
            sql += s.MaDocGia + ", ";
            sql += "'" + s.NgayMuon + "', ";
            sql += "'" + s.NgayHetHan + "')";
            if (pms.Insert(sql))
                return PhieuMuonSachBUSResult.OK;
            return PhieuMuonSachBUSResult.DAOERROR;
        }
        public static void LayMaPhieuMuonSachCuaDocGiaDo(string maDocGia, string maSach, out int maPhieu)
        {
            DAO.PhieuMuonSach dao = new DAO.PhieuMuonSach();
            string sql = "select PhieuMuonSach.MaPhieu";
            sql += " FROM  PhieuMuonSach,ChiTietPhieuMuonSach,Sach,DocGia";
            sql += " where PhieuMuonSach.MaPhieu=ChiTietPhieuMuonSach.MaPhieuMuon";
            sql += " And Sach.MaSach=ChiTietPhieuMuonSach.MaSach";
            sql += " And DocGia.MaDocGia=PhieuMuonSach.MaDocGia";
            sql += " AND DocGia.MaDocGia='" + maDocGia+"'";
            sql += " And Sach.MaSach='" + maSach+"'";
            DataTable dt = dao.Select(sql);
            maPhieu = int.Parse(dt.Rows[0][0].ToString());

        }
       
    }
}
